<?php

/**
 * @Author: Wang Chunsheng 2192138785@qq.com
 * @Date:   2020-03-09 23:19:04
 * @Last Modified by:   Wang Chunsheng 2192138785@qq.com
 * @Last Modified time: 2020-03-30 13:39:07
 */

namespace common\addons\diandi_shop\api;

use Yii;
use api\controllers\AController;
use common\addons\diandi_shop\models\DdDiandiShopStore;
use common\helpers\ImageHelper;
use common\helpers\MapHelper;
use common\helpers\ResultHelper;

class StoreController extends AController
{
    public $modelClass = '\common\models\DdGoods';
    protected $authOptional = ['info'];

    /**
     * @SWG\Get(path="/diandi_shop/store/info",
     *     tags={"商家"},
     *     summary="商家信息.",
     *     @SWG\Response(
     *         response = 200,
     *         description = "商家信息",
     *     )
     * )
     */
    public function actionInfo()
    {
        $info = Yii::$app->settings->getAllBySection('DiandiShopStroe');
        $info['logo'] = ImageHelper::tomedia($info['logo']);
        $info['banner'] = ImageHelper::tomedia($info['banner']);
        $surroundings = explode(',', $info['surroundings']);
        $certificates = explode(',', $info['certificate']);
        if ($surroundings) {
            $surroundings = array_chunk(ImageHelper::tomedia($surroundings), 2);
        }
        $info['surroundings'] = $surroundings;

        if ($certificates) {
            $certificate = array_chunk(ImageHelper::tomedia($certificates), 2);
        }
        $info['certificate']  = $certificate;

        $info['hotSearch'] = explode(',', $info['hotSearch']);
        $info['wxappName'] = Yii::$app->settings->get('Wxapp', 'name');

        return ResultHelper::json(200, '获取成功', $info);
    }

    /**
     * @SWG\Get(path="/diandi_shop/store/distance",
     *     tags={"商家"},
     *     summary="计算与商家的距离",
     *     @SWG\Response(
     *         response = 200,
     *         description = "计算与商家的距离",
     *     ),
     *     @SWG\Parameter(
     *      in="query",
     *      name="lat",
     *      type="string",
     *      description="纬度",
     *      required=true,
     *    ),
     *     @SWG\Parameter(
     *      in="query",
     *      name="lng",
     *      type="string",
     *      description="经度",
     *      required=true,
     *    ),
     * )
     */
    public function actionDistance()
    {
        $lng1 = Yii::$app->request->get('lng');
        $lat1 = Yii::$app->request->get('lat');
        $lng_lat = Yii::$app->settings->get('DiandiShopStroe', 'lng_lat');
        $distance = Yii::$app->settings->get('DiandiShopStroe', 'distance');

        list($lng2, $lat2) = explode(',', $lng_lat);
        $data = MapHelper::getdistance($lng1, $lat1, $lng2, $lat2);

        $is_distance = $data / 1000 > $distance ? 0 : 1;

        return ResultHelper::json(200, '获取成功', ['distance' => $data, 'is_distance' => $is_distance]);
    }
}
